from datetime import datetime

from fastapi import Request
from sqlalchemy.orm import Session

from core.dao.cms_alert_message_dao import CmsAlertMessageDao
from core.entity.vo.common import CrudResponse
from core.entity.vo.view_models import CmsAlertMessageVO, CmsAlertMessageQueryPage


class CmsAlertMessageService:
    """
    高德终端service
    """

    @classmethod
    def get_cms_alert_message_by_id(cls, result_db: Session, id: int):
        """
        获取参数配置列表信息service
        :param result_db: orm对象
        :param id: id
        :return: 参数配置列表信息对象
        """
        cms_alert_message = CmsAlertMessageDao.get_cms_alert_message_by_id(result_db, id)

        return cms_alert_message

    @classmethod
    def get_cms_alert_message(cls, result_db: Session, cms_alert_message_query: CmsAlertMessageQueryPage):
        """
        获取参数配置列表信息service
        :param result_db: orm对象
        :param fence_id: 围栏编码
        :param car_number: 车牌号
        :param begin_time: 开始时间
        :param end_time: 结束时间
        :return: 参数配置列表信息对象
        """
        cms_alert_message = CmsAlertMessageDao.get_cms_alert_message(result_db, cms_alert_message_query)
        total = CmsAlertMessageDao.get_cms_alert_message_total(result_db, cms_alert_message_query)
        return cms_alert_message, total

    @classmethod
    async def add_cms_alert_message(cls, result_db: Session, cms_alert_message: CmsAlertMessageVO):
        """
        新增参数配置信息service
        :param request: Request对象
        :param result_db: orm对象
        :param cms_alert_message: 新增参数配置对象
        :return: 新增参数配置校验结果
        """
        try:
            CmsAlertMessageDao.add_cms_alert_message_dao(result_db, cms_alert_message)
            result_db.commit()
            result = dict(is_success=True, message='新增成功')
        except Exception as e:
            result_db.rollback()
            result = dict(is_success=False, message=str(e))
        return CrudResponse(**result)

    @classmethod
    async def edit_cms_alert_message(cls, request: Request, result_db: Session, cms_alert_message: CmsAlertMessageVO):
        """
        编辑参数配置信息service
        :param request: Request对象
        :param result_db: orm对象
        :param cms_alert_message: 编辑参数配置对象
        :return: 编辑参数配置校验结果
        """
        try:
            edit_post = cms_alert_message.dict(exclude_unset=True)
            CmsAlertMessageDao.edit_cms_alert_message_dao(result_db, edit_post)
            result_db.commit()
            result = dict(is_success=True, message='修改成功')
        except Exception as e:
            result_db.rollback()
            result = dict(is_success=False, message=str(e))
        return CrudResponse(**result)

    @classmethod
    def delete_cms_alert_message(cls, result_db: Session, id: int):
        """
        删除菜单信息service
        :param result_db: orm对象
        :param id: id
        :return: 删除终端
        """
        if id > 0:
            try:
                CmsAlertMessageDao.delete_cms_alert_message_dao(result_db, id)
                result_db.commit()
                result = dict(is_success=True, message='删除成功')
            except Exception as e:
                result_db.rollback()
                result = dict(is_success=False, message=str(e))
        else:
            result = dict(is_success=False, message='传入id为空')
        return CrudResponse(**result)
